package cn.sxw.android.base.utils;

import android.os.Build;
import android.os.Looper;
import android.util.Log;

import org.apache.log4j.Logger;

import cn.sxw.android.BuildConfig;

/**
 * Created by ZengCS on 2017/9/5.
 * E-mail:zcs@sxw.cn
 * Add:成都市天府软件园E3-3F
 */

public class JLogUtil {
    public static final String TAG = "Log/Sxjy";
    public static final String TAG_STEP = "Debug/Step";
    public static final String TAG_DOWNLOAD = "Debug/Download";
    public static final String TAG_INSTALL = "Debug/Install";
    public static final String TAG_UNINSTALL = "Debug/Uninstall";
    public static final String TAG_PC = "Debug/ConnectPC";
    public static final String TAG_HB = "Debug/Heartbeat";
    public static final String TAG_SHOW = "Debug/StudentShow";
    public static final String TAG_NEW_C5 = "Debug/NewC5";
    public static final String TAG_KNOX = "Debug/KNOX";
    public static final String TAG_FIREWALL = "Debug/Firewall";
    public static final String TAG_DOCTOR = "Debug/Doctor";
    public static final String TAG_ROBOT = "Log/RobotSDK";
    public static final String TAG_HTTP = "Debug/Http";
    public static final String TAG_MQTT = "Debug/MQTT";

    private static Logger log = Logger.getLogger(TAG);
    private static boolean isDebug = true;
    private static boolean useLog4j = !BuildConfig.DEBUG;
    private static boolean enableLogcat = BuildConfig.DEBUG;// 允许使用logcat

    /**
     * 切换Logcat开关状态
     */
    public static void switchLogcat(boolean enable) {
        isDebug = enable;
        enableLogcat = enable;
        w("Logcat已开启，重启平板或者桌面后自动关闭");
    }

    public static void doctor(String log) {
        i(TAG_DOCTOR, log);
    }

    public static void logDownload(String log) {
        i(TAG_DOWNLOAD, log);
    }

    public static void logConnectPC(String log) {
        w(TAG_PC, log);
    }

    public static void logKnox(String log) {
        i(TAG_KNOX, log);
    }

    public static void logFirewall(String log) {
        w(TAG_FIREWALL, log);
    }

    public static void logStudentShow(String log) {
        w(TAG_SHOW, log);
    }

    public static void logConnectPC_I(String log) {
        i(TAG_PC, log);
    }

    public static void logConnectPC_E(String log) {
        e(TAG_PC, log);
    }

    public static void logHeartbeat(String log) {
        w(TAG_HB, log);
    }

    public static void logConnectPC(String tag, String log) {
        w(tag, log);
    }

    public static void logInstall(String log) {
        i(TAG_INSTALL, log);
    }

    public static void logHonor(String log) {
        i("Dev/Honor", Build.MODEL + "->" + log);
    }

    public static void logHonorE(String log) {
        e("Dev/Honor", Build.MODEL + "->" + log);
    }

    public static void logNewC5(String log) {
        i(TAG_NEW_C5, log);
    }

    public static void logRobot(String log) {
        d(TAG_ROBOT, log);
    }

    public static void dev(String tag, String msg) {
        if (BuildConfig.DEBUG) {
            i(tag, "[DEBUG]" + msg);
        }
    }

    public static void e(String tag, String msg) {
        if (enableLogcat) {
            boolean isMain = Looper.myLooper() == Looper.getMainLooper();
            if (isMain) {
                Log.e(tag, "[@Main Thread]" + msg);
            } else {
                Log.e(tag, "[Child Thread]" + msg);
            }
        }

        if (useLog4j)
            log.error((tag.equals(TAG) ? "" : "[" + tag + "]") + msg);
    }

    public static void e(Throwable e) {
        log.error("------------------------------------------------------------");
        log.error(Log.getStackTraceString(e));
        log.error("------------------------------------------------------------");
    }

    public static void e(String tag, String msg, Throwable e) {
        if (enableLogcat) {
            Log.e(tag, msg, e);
        }
        if (useLog4j)
            if (e != null)
                e(e);
            else
                log.error((tag.equals(TAG) ? "" : "[" + tag + "]") + msg);
    }

    public static void w(String tag, String msg) {
        if (enableLogcat) {
            boolean isMain = Looper.myLooper() == Looper.getMainLooper();
            if (isMain) {
                Log.w(tag, "[@Main Thread]" + msg);
            } else {
                Log.w(tag, "[Child Thread]" + msg);
            }
        }
        if (useLog4j) {
            log.warn((tag.equals(TAG) ? "" : "[" + tag + "]") + msg);
        }
    }

    public static void i(String tag, String msg) {
        if (isDebug && enableLogcat) {
            boolean isMain = Looper.myLooper() == Looper.getMainLooper();
            if (isMain) {
                Log.i(tag, "[@Main Thread]" + msg);
            } else {
                Log.i(tag, "[Child Thread]" + msg);
            }
        }
        if (useLog4j)
            log.info((tag.equals(TAG) ? "" : "[" + tag + "]") + msg);
    }

    public static void d(String tag, String msg) {
        if (isDebug && enableLogcat) {
            boolean isMain = Looper.myLooper() == Looper.getMainLooper();
            if (isMain) {
                Log.d(tag, "[@Main Thread]" + msg);
            } else {
                Log.d(tag, "[Child Thread]" + msg);
            }
        }
        if (useLog4j)
            log.info((tag.equals(TAG) ? "" : "[" + tag + "]") + msg);
    }

    public static void v(String tag, String msg) {
        if (isDebug && enableLogcat) {
            boolean isMain = Looper.myLooper() == Looper.getMainLooper();
            if (isMain) {
                Log.v(tag, "[@Main Thread]" + msg);
            } else {
                Log.v(tag, "[Child Thread]" + msg);
            }
        }
        if (useLog4j)
            log.info((tag.equals(TAG) ? "" : "[" + tag + "]") + msg);
    }

    // 无TAG方法
    public static void e(String msg) {
        e(TAG, msg);
    }

    public static void e(String msg, Throwable e) {
        e(TAG, msg, e);
    }

    public static void w(String msg) {
        w(TAG, msg);
    }

    public static void i(String msg) {
        i(TAG, msg);
    }

    public static void d(String msg) {
        d(TAG, msg);
    }

    public static void v(String msg) {
        v(TAG, msg);
    }

    public static void methodStart(String msg) {
        w(TAG, "******************** Start " + msg + " ********************");
    }

    public static void methodStep(String msg) {
        w(TAG, "* --> " + msg);
    }

    public static void methodStepWithTime(String msg) {
        w(TAG_STEP, "[" + System.nanoTime() + "] --> " + msg);
    }

    public static void methodStepError(String msg) {
        e(TAG, "* !!! " + msg);
    }

    public static void methodEnd(String msg) {
        w(TAG, "******************** End " + msg + " ********************");
    }

    public static void methodStartHttp(String msg) {
        w(TAG_HTTP, "******************** Start " + msg + " ********************");
    }

    public static void logMQTT(String msg) {
        w(TAG_MQTT, msg);
    }

    public static void methodStepHttp(String msg) {
        w(TAG_HTTP, "* --> " + msg);
    }

    public static void onReceiveEvent(String msg) {
        w(TAG, "【EventBus-Receive】" + msg);
    }

    public static void postEvent(String msg) {
        w(TAG, "【EventBus-Post】" + msg);
    }
}
